import {getVipPowers} from "@/api/vip.js";
import	{toast} from "@/utils/util.js"

export const CODES = {
	SHOPPING_SALE:"shopping_sale",	
	DYNAMIC_COUNT:"dynamic_count",	
	DYNAMIC_TOP:"dynamic_top",	
	HEAD_TAG:"head_tag",	
	ACCESS_RECORD:"access_record",	
	SUPER_PRAISE:"super_praise",	
	DYNAMIC_EXPOSURE:"dynamic_exposure", 
	REMOVE_ADVERTISE:"remove_advertise",	
	ATTENTION_COUNT:"attention_count",	
	DIRECTED_LOO:"directed_look",	
	SELF_DYNAMIC_TOP:"self_dynamic_top",	
	VIP_FIRST_OPEN_CAN:"vip_first_open_can"	
}
export const loaded = async ()=>{
	try{
		const ruleMap = getApp().globalData.vipRuleMap
		if(ruleMap&&ruleMap.size){
			return ruleMap
		}
		const res = await getVipPowers();
		Object.entries(res).forEach(([key,value])=>{
			if(Array.isArray(value)){
				const map = new Map();
				value.forEach(item=>{
					map.set(item.ruleCode,item)
				})
				ruleMap.set(key,map)
			}
		})
		return ruleMap
	}catch(e){
		console.error(e)
		
	}
};

export const getRule = (vipCode,ruleCode) =>{
	const cache = getApp().globalData.vipRuleMap
	return cache.get(vipCode)&&cache.get(vipCode).get(ruleCode)&&cache.get(vipCode).get(ruleCode).data
}
export const isVip=(endDate)=>{
	if(!endDate){
		return false
	}
	const now = Date.now()
	let expire = null
	if(typeof endDate == 'string'){
		expire = new Date(endDate.replace(/-/g, '/')).getTime()
	}
	if(endDate instanceof Date){
		expire = endDate.getTime()
	}
	return expire>now
}
export default{
	getRule,
	isVip
}